"""
    自定义分页组件
        request: 用户提交的数据，内含：
            'page'： 当前要显示的页码
            'pagesize'： 每一页的记录条数
        data_list_all: 需要分页的所有记录
"""
from django.utils.safestring import mark_safe


class Pagination(object):
    # 以“__”开头的变量为类的私有变量，外部不能访问
    def __init__(self, request, data_list_all):
        # 要显示的页码
        self.page = int(request.GET.get('page', 1))
        # 每页的条数,缺省10条，函数的局部变量
        self.page_size = int(request.GET.get('pagesize', 10))

        # 总页码数
        self.__pages, div = divmod(data_list_all.count(), self.page_size)
        if div:
            self.__pages += 1
        if self.page > self.__pages:
            self.page = self.__pages

        # 第page的记录，类的公有变量，但只有类实例化后才能访问
        self.start_recno = (self.page - 1) * self.page_size
        self.end_recno = self.page * self.page_size
        self.data_list = data_list_all[self.start_recno:self.end_recno]
        # 生成页码,只显示当前页的前5和后5页
        plus = 5
        if self.__pages < 2 * plus + 1:
            self.__start_page = 1
            self.__end_page = self.__pages
        else:
            # 当前页<5时
            if self.page <= plus:
                self.__start_page = 1
                self.__end_page = 2 * plus
            else:
                # 当前页 > 5
                # 当前页+5 > 总页数
                if (self.page + plus) > self.__pages:
                    self.__start_page = self.__pages - 2 * plus
                    self.__end_page = self.__pages
                else:
                    self.__start_page = self.page - plus
                    self.__end_page = self.page + plus

    # 返回分页的HTML代码，类的仅有函数
    def html(self):
        # 首页
        page_str_list = [
            '<li><a href="?page=1&pagesize={}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>'.format(
                self.page_size)]
        # 中间页
        for i in range(self.__start_page, self.__end_page + 1):
            if i == self.page:
                ele = '<li class="active"><a href="?page={}&pagesize={}">{}</a></li>'.format(i, self.page_size, i)
            else:
                ele = '<li><a href="?page={}&pagesize={}">{}</a></li>'.format(i, self.page_size, i)
            page_str_list.append(ele)
        # 尾页
        page_str_list.append(
            '<li><a href="?page={}&pagesize={}" aria-label="Next"><span aria-hidden="true">尾页</span></a></li>'.format(
                self.__pages, self.page_size))
        page_string = "".join(page_str_list)
        # 下拉菜单
        dropdown_list = []
        for j in [10, 20, 30, 40, 50, 100, 200, 500, 1000]:
            dropdown_list.append(
                '<li><a href="?page={0}&pagesize={1}">{1}条</a></li>'.format(self.page, j))
        dropdown_str = '''
                        <div class="dropup btn-group">
                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                    aria-haspopup="true" aria-expanded="false">
                                {0}条 <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu">
                                {1}
                            </ul>
                        </div>
                        '''.format(self.page_size, ''.join(dropdown_list))

        page_string = mark_safe(page_string + dropdown_str)
        return page_string
